<template>
	<view class="mt-2" >
		<view class="bg-white p-2 mx-2 " style="width:670upx;line-height: 80upx;">
			<view>
				<text>模拟试卷名称</text><text style="color: firebrick;">*</text>
			</view>
			<view style="height:80upx;line-height: 80upx;color: #425ca1;">
				<input  placeholder="请输入模拟试卷名称" v-model="paperName"/>
			</view>
			
			<view>
				<text>考试限时(分钟)</text><text style="color: firebrick;">*</text>
			</view>
			<view  style="height:80upx;line-height: 80upx;color: #425ca1;">
				<input type="number" v-model="duration"/>
			</view>
			
			<view style="font-size:20upx;color:red;">
				<text>随机抽取题库试题，组成100分试卷，进入模块练习</text>
			</view>
			
			<view class="mt-4 d-flex flex-row flex-nowrap">
				<view class="w-30 pr-2" >
					<button class="gen_btn" @click="genPaper">生成试卷</button>
				</view>
				<view class="w-70 pl-2">
					<button class="gen_entry_btn" @click="genPaperAndEntry">生成试卷并进入模拟</button>
				</view>
			</view>
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				paperName: '',
				duration: '90',
			}
		},
		async onLoad() {
			await this.getPaperName()
		},
		methods: {
			
			// 自动生成试卷名称
			getPaperName(){
				let url = '/moni/getPaperName'
				let apply = uni.getStorageSync("staff_apply")
				let param = {examType: apply.examType}
				this.$H.post(url, param, {token: true}).then(res=>{
					if(res && res.code == 200){
						// 试卷名称
						this.paperName = res.result
					}
					
				}).catch( err =>{
					if(err.msg){
						uni.showToast({
							title: err.msg,
							icon: 'none'
						})
					}
				})
				
			},
			// 生成试卷
			genPaper(){
				
				if(!this.paperName){
					uni.showToast({
						title: '请输入模拟试卷名称',
						icon: 'none'
					})
					return
				}
				
				if(!this.duration){
					uni.showToast({
						title: '请输入考试限时(分钟)',
						icon: 'none'
					})
					return
				}
				
				
				let url = '/moni/createSimulatePaper'
				let apply = uni.getStorageSync("staff_apply")
				let param = {paperName: this.paperName, duration: this.duration, examType: apply.examType}
				uni.showLoading({
					title: '正在生成试卷...',
					icon: 'none'
				})
				this.$H.post(url, param, {token: true}).then(res=>{
					if(!res || res.code != 200){
						return uni.showToast({
							title: '创建试卷失败'
						})
					}
					
					uni.hideLoading()
					
					setTimeout(()=>{
						uni.switchTab({
							url: "/pages/moni/index"
						})
					}, 500)
				}).catch( err =>{
					uni.hideLoading()
					if(err.msg){
						uni.showToast({
							title: err.msg,
							icon: 'none'
						})
					}
				})
			},
			// 生成试卷，进入模拟考试 
			genPaperAndEntry(){
				
				if(!this.paperName){
					uni.showToast({
						title: '请输入模拟试卷名称',
						icon: 'none'
					})
					return
				}
				
				if(!this.duration){
					uni.showToast({
						title: '请输入考试限时(分钟)',
						icon: 'none'
					})
					return
				}
				
				let url = '/moni/generateSimulateExam'
				let apply = uni.getStorageSync("staff_apply")
				let param = {paperName: this.paperName, duration: this.duration, examType: apply.examType}
				
				uni.showLoading({
					title: '正在生成试卷...',
					icon: 'none'
				})
				this.$H.post(url, param, {token: true}).then(res=>{
					if(!res || res.code != 200){
						return uni.showToast({
							title: '创建试卷失败'
						})
					}
					let result = res.result
					uni.hideLoading()
					// 进入考试页面
					setTimeout(()=>{
						uni.navigateTo({
							url: "/pages/moni/answer?answerPaperId=" + result.id + "&staffId=" + result.staffId + "&paperId=" + result.paperId + "&remainedTime=" + result.remainedTime
						})
					}, 500)
				}).catch( err =>{
					uni.hideLoading()
					if(err.msg){
						uni.showToast({
							title: err.msg,
							icon: 'none'
						})
					}
				})
			}
		}
	}
</script>

<style>
	page{
		height: 100%;
		background: #f1f1f1;
	}
	
	.gen_btn{
		height:70upx;line-height: 70upx; border-radius: 35upx;color:#ffffff;background-color: #425ca1;font-size:24upx;
	}
	
	.gen_entry_btn{
		height:70upx;line-height: 70upx; border-radius: 35upx;color:#ffffff;background-color: #e56509;font-size:24upx;
	}

</style>
